<?php

namespace app\api\controller;


use app\api\model\GlobalConfig;
use ReflectionClass;

class Base extends \app\BaseController
{
    //用户信息
    protected $_global_config;
    protected $_user_info;

    protected $params;

    protected $header;

    /**
     * 初始化
     * @method GET
     */
    public function initialize()
    {
        global $globalConfig;
        $this->crossDomain();
        $this->_global_config = $globalConfig = GlobalConfig::getAllConfig();
        $checkParams = $this->getDocCommentParams(['method', 'needLogin']);
        $this->checkRequestMethod($checkParams['method']);
        $this->params = $this->request->param();
        $this->header = $this->request->header();
        RecordLog(['params' => $this->params, 'headers' => $this->header], request()->controller() . '/' . request()->action());
    }

    /**
     * 获取文档设置
     * @param array $params
     * @return array
     * @throws \ReflectionException
     */
    private function getDocCommentParams(array $params)
    {
        $className = '\app\api\controller\\' . request()->controller();
        $reflection = new ReflectionClass($className);
        $actions = $reflection->getMethods();
        $data = [];
        foreach ($params as $param) {
            $data[$param] = '';
        }
        foreach ($actions as $action) {
            if ($action->name == request()->action()) {
                $string = $action->getDocComment();
                foreach ($params as $param) {
                    preg_match_all('/@' . $param . '(.*?)[\r\n|\n]/', $string, $arr);
                    $data[$param] = trim($arr[1][0] ?? "");
                }
            }
        }
        return $data;
    }

    /**
     * 请求方式验证
     */
    private function checkRequestMethod($method)
    {
        switch ($method) {
            case 'GET':
                if (!$this->request->isGet())
                    apiReturn(404, '仅支持GET请求');
                break;
            case 'POST':
                if (!$this->request->isPost())
                    apiReturn(404, '仅支持POST请求');
                break;
            case 'PUT':
                if (!$this->request->isPut())
                    apiReturn(404, '仅支持PUT请求');
                break;
            case 'DELETE':
                if (!$this->request->isDelete())
                    apiReturn(404, '仅支持DELETE请求');
                break;
            case 'AJAX':
                if (!$this->request->isAjax())
                    apiReturn(404, '仅支持AJAX请求');
                break;
            case 'MOBILE':
                if (!$this->request->isMobile())
                    apiReturn(404, '仅支持手机请求');
                break;
            default:
                break;
        }
    }

    //跨域处理
    protected function crossDomain()
    {
        header('content-type:text/html;charset=utf-8');
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,timestamp");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
    }
}